package me.cloudcat.develop;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author: jiafeim12345@163.com
 * @Date: 2018/3/18/018 18:45
 */
@SpringBootApplication
//@MapperScan(basePackages = "me.cloudcat.develop.dao")
public class Application implements CommandLineRunner {

  public static Logger logger = LoggerFactory.getLogger(Application.class);

  public static void main(String[] args) {
    SpringApplication.run(Application.class);
  }

  @Autowired
  private KafkaTemplate<String, String> template;

  private final CountDownLatch latch = new CountDownLatch(3);

  @Override
  public void run(String... args) throws Exception {
    this.template.send("myTopic", "foo1");
    this.template.send("myTopic", "foo2");
    this.template.send("myTopic", "foo3");
    latch.await(60, TimeUnit.SECONDS);
    logger.info("All received");
  }

  @KafkaListener(topics = "myTopic")
  public void listen(ConsumerRecord<?, ?> cr) throws Exception {
    logger.info(cr.toString());
    latch.countDown();
  }
}
